package 链表与双指针.t01;


import java.util.Stack;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
class ListNode {
    int val;
    ListNode next;
    ListNode(int x) {
        val = x;
        next = null;
    }
}
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        Stack<ListNode> AStack = new Stack<>();
        Stack<ListNode> BStack = new Stack<>();
        ListNode tmpA = headA;
        ListNode tmpB = headB;
        while(tmpA != null) {
            AStack.push(tmpA);
            tmpA = tmpA.next;
        }
        while(tmpB != null) {
            BStack.push(tmpB);
            tmpB = tmpB.next;
        }
        ListNode tmp = null;
        while((!AStack.isEmpty())
        &&(!BStack.isEmpty())
        &&(AStack.peek() == BStack.peek())) {
            tmp = AStack.pop();
            BStack.pop();
        }
        return tmp;
    }
}


